package com.cfets.annualAssessment.backgroundController;

import com.cfets.annualAssessment.entity.Account;
import com.cfets.annualAssessment.entity.Role;
import com.cfets.annualAssessment.entity.TableContainer;
import com.cfets.annualAssessment.service.AccountService;
import com.cfets.annualAssessment.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by ShenYiChou on 2017/2/7.
 */
@Controller
@RequestMapping("/admin/account")
public class AccountController {
    @Autowired
    private AccountService accountService;
    @Autowired
    private RoleService roleService;

    @RequestMapping("/index")
    public String index(Model model){
        return "account";
    }

    @RequestMapping(value = "/Get",method = RequestMethod.POST)
    public ResponseEntity<?> accountList(Account account){
        List<Account> accountList = accountService.getAccountPage(account);
        TableContainer tableContainer = new TableContainer();
        tableContainer.setAaData(accountList);
        tableContainer.setiTotalDisplayRecords(account.getTotalCount());
        tableContainer.setiTotalRecords(account.getTotalCount());
        return new ResponseEntity<>(tableContainer, HttpStatus.OK);
    }

    @PostMapping
    public ResponseEntity<?> createOrUpdate(@Valid Account account, BindingResult result) {
        if (result.hasErrors()) {
            return new ResponseEntity<>(result.getAllErrors(), HttpStatus.OK);
        }
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=new Date();
        String str=sdf.format(date);
        account.setPostTime(str);
        boolean flag = false;
        if(account.getId()!=null && account.getId()!=0){
            flag = (this.accountService.updateAccount(account)!=null);
        }else {
            //-1 用户登录名重复
            Account account1 = new Account();
            account1.setAccountName(account.getAccountName());
            if(!accountService.getAccountList(account1).isEmpty()){
                return new ResponseEntity<>(-1, HttpStatus.OK);
            }
            account.setNewpassword("111111");//默认密码
//            account.setStatus(3);//默认启动
            flag = (this.accountService.newAccount(account)!=null);
        }
        return new ResponseEntity<>(flag, HttpStatus.OK);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteAccount(@PathVariable("id") int id){
        boolean flag = this.accountService.deleteAccount(id);
        return new ResponseEntity<>(flag, HttpStatus.OK);
    }

    @RequestMapping(value = "/unlock",method = RequestMethod.POST)
    public ResponseEntity<?> unlockAccount(long id){
        Account account = accountService.getAccountById(id);
        account.setStatus(3);//3为启用状态
        boolean flag = (this.accountService.updateAccount(account)!=null);
        return new ResponseEntity<>(flag, HttpStatus.OK);
    }

    @RequestMapping(value = "/reset",method = RequestMethod.POST)
    public ResponseEntity<?> resetAccount(long id){
        Account account = accountService.getAccountById(id);
        account.setNewpassword("111111");//可以改成随机的
        boolean flag = (this.accountService.updateAccount(account)!=null);
        return new ResponseEntity<>(flag, HttpStatus.OK);
    }

    @ModelAttribute("roles")
    public List<Role> getRoles(){
        List<Role> roleList = null;
        Role role = new Role();
        roleList = roleService.getRole(role);
        return roleList;
    }
}
